/*
 * @Descripttion:
 * @Author: jhw
 * @Date: 2023-07-27 11:32:48
 * @LastEditors: jhw
 * @LastEditTime: 2023-07-28 15:33:56
 */
import { commonApi } from '@/api';
import { UploadRequestOptions } from 'element-plus';
import {} from 'vue';
const { uploadFile } = commonApi;
export class Upload {
  options: any;
  file: UploadRequestOptions;

  constructor(file: UploadRequestOptions, options?: any) {
    this.file = file;
    this.options = options;
  }

  // 上传
  upload() {
    let params = {
      data: { file: this.file.file, filename: this.file.filename },
      onUploadProgress: (e) => {
        this.onProgress(Math.ceil((e.loaded / e.total) * 100));
      },
    };
    return new Promise((resolve, reject) => {
      uploadFile(params)
        .then((res: any) => {
          resolve(res);
        })
        .catch((err: any) => {
          reject(err);
        });
    });
  }

  onProgress = (e: any) => {
    this.options.onProgress(e);
  };
}
